<template>
    <div class="bigsearchbox" :style="{height:searchgao+'px'}">
        <div v-show="!xs">
            <div class="searchtop">
                <div class="sshuru">
                    <svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-sousuo"></use>
                    </svg>
                    <input type="text" placeholder="搜索书籍" v-model="sousuo" autofocus ref="input"  id="shu">
                    <span class="qc" @click="qinchu">×</span>
                </div>
                <div @click="fanhui()" class="qx">
                    取消
                </div>
            </div>
            <div class="searchdown">
                <ul class="searchend" v-show="cha">
                    <li v-for="(item,index) in searcharr" :key="index">
                        <div @click="go(item.bookId,item.title)">
                            <p class="searchsong">
                                <svg class="icon" aria-hidden="true">
                                    <use xlink:href="#icon-shu1"></use>
                                </svg>
                                <span>{{item.title}}</span>
                                ——
                                <span>{{item.author}}</span> 
                            </p>
                        </div>
                    </li>
                    <li v-show="unde" class="nofind">啥都找不到哦</li>
                    <li class="finding">
                        <div class="loading">
                            <loading v-show="ssz"></loading>
                        </div>
                    </li>
                </ul>
                <div v-show="!cha">
                    <h4 class="searchtou">大家都在搜</h4>
                    <ul class="hotsearch">
                        <li v-for="item in hot" :key="item.id">
                            <div @click="diancha(item.name)">
                                {{item.name}}
                            </div>
                        </li>
                    </ul>
                </div>
            </div>            
        </div>
        <loading v-show="xs" class="top"></loading>
    </div>
</template>
<script>
import loading from "../small/loading.vue"
export default {
    name:"search",
    data(){
        return {
            sousuo:'',
            hot:[],
            his:[],
            xs:true,
            ssz:false,
            cha:false,
            tim:null,
            unde:false,
            searcharr:[],
            searchgao:0,
        }
    },
    components:{
        loading
    },
    created(){
        this.getdata()
        this.$store.commit('changenavshow', false)
        this.searchgao = document.documentElement.clientHeight || documentbody.clientHeight;
    },
    watch:{
        'sousuo'(){
            if(this.sousuo == ''){
                this.cha = false
            }else{
                this.ssz = true
                this.cha = true
                this.searcharr = []
                this.jieliu()
            }
            this.unde = false
        },
        "his"(){
            if(typeof this.his == "string"){
                this.his = []
            }
        }
    },
    methods:{
        qinchu(){
            this.sousuo = ''
        },
        fanhui(){
            this.$router.go(-1)
        },
        getdata(){
            axios.get(`https://apis.netstart.cn/yunyuedu/search/getHotWords`).then(
                res=>{
                    // console.log(res)
                    this.hot = res.data.list
                    this.xs = false
                },
                err=>{
                    console.log(err)
                }
            )
        },
        getsearch(){
            axios.get(`https://apis.netstart.cn/yunyuedu/search/book.json?search=${this.sousuo}`).then(
                res=>{
                    // console.log(res)
                    this.searcharr = res.data.data.books
                    this.ssz = false
                    this.unde = false
                },
                err=>{
                    console.log(err)
                }
            )
        },
        jieliu(){
            clearTimeout(this.tim)
            this.ssz = true
            this.tim = null
            this.tim = setTimeout(()=>{
                // console.log(this.sousuo)
                if(this.sousuo !== ''){
                    this.getsearch()
                }
                clearTimeout(this.tim)
                this.tim = null
                // this.unde = false
            },2000)
        },
        go(id,title){
            this.$store.commit("book/changenowbookid",id)
            this.$store.commit("book/changenowbooktitle",title)
            this.$router.push("/book")
        },
        diancha(value){
            this.sousuo = value
        }
    }
}
</script>
<style lang="less" scoped>
@import '../other/all.less';
a{
    text-decoration: none;
    color:#a3a3a3 ;
}
ul,li{
    margin: 0;
    padding: 0;
    list-style: none;
}
.bigsearchbox{
    height: 100%;
    .searchtop{
        padding: 0px 8%;
        display: flex;
        height:50px;
        // justify-content: space-between;
        .qx{
            margin-left: 5%;
            margin-top: 9%;
            color: @allcolor1;
            font-size: 0.5rem;
            font-family: 'YouYuan';
            font-weight: 600;
        }
    }
    .searchdown{}
}
.sshuru{
    position: relative;
    margin-top:6.5%;
    width: 80%;
    // margin-left: 5%;
    svg{
        position: absolute;
        top:0px;
        left:0px;
        color: @allcolor2;
        font-size: 0.55rem;
        top: 25%;
        left: 2%;
    }
    input{
        border: 1px solid @allcolor2;
        outline: none;
        width: 100%;
        height: 25px;
        color:@allcolor2;
        padding-left:30px;
        box-sizing: border-box;
        border-radius: 15px;
        padding-right:30px;
        // background: rgba(255, 255, 255, 0.3);
        &::-webkit-input-placeholder{
            // color:rgba(255, 255, 255, 0.698);
            color: @allcolor3;
        }
    }
    span{
        display: inline-block;
        position: absolute;
        top: 20%;
        right: 2%;
        width: 20px;
        line-height: 20px;
        font-size: 25px;
        height: 20px;
        border-radius: 50%;
        // background-color: #eee;
        color: @allcolor1;
        // text-shadow: 0px 0px 3px #bbb;
        text-align: center;
    }
}
.searchdown{
    padding: 0px 10%;
    position: relative;
}
.searchend{
    // position: absolute;
    background-color: white;
    padding-bottom: 30px;
    // top: -10%;
    // left: 10%;
    // width: 80%;
    li{
        width: 80%;
        padding-bottom: 0px;
        border-bottom: 1px solid #ccc;
        font-size:0.4rem;
        text-align: left;
        color:@allcolor1;
    }    
}
.searchsong{
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    svg{
        margin-right: 5px;
    }
    span{

    }
}
.searchtou{
    font-family: 'YouYuan';
    color: #646464;
    display: flex;
    font-size: 0.5rem;
    justify-content: space-between;
    span{

        svg{
            margin-right: 20px;
            font-size: 0.8rem;
            color: #646464;
        }
    }
}
.hotsearch{
    display:flex;
    flex-wrap: wrap;
    li{
        background-color:  #f4f4f4;
        border-radius: 50px;
        padding: 5px;
        color: #a3a3a3;
        font-size: 0.4rem;
        margin-right: 10px;
        margin-bottom: 10px;
        font-family: "宋体";
        &:first-child{
            background-color:  @allcolor3;
            font-weight: 600;
            div{
                color:@allcolor2 !important;
            }
        }
        &:nth-child(2){
            background-color:  @allcolor3;
            font-weight: 600;
            div{
                color:@allcolor2 !important;
            }
        }
    }
    
}
.nofind{
    font-size: 2rem;
    color: #a3a3a3;
    margin-top: 20px;
    text-align: center;
    border:0px solid transparent !important;
}
.top{
    height: 100%;
    width:100%;
}
.finding{
    width: 90% !important;
    height:200px;
    border:0px solid transparent !important;
    font-size: 13px;
    position: relative;
    .loading{
        width: 100%;
        height: 100%;
        position: absolute;
        top:0px;
        left:-10%;
    }
}
</style>